/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import core.Node;
import core.Port;
import java.io.Serializable;
import java.util.HashMap;
import model.graphic.objects.GConnection;
import model.graphic.objects.Graphical;

/**
 *
 * @author Jelle
 */
public class BidirectionalHashMap implements Serializable {

    private static volatile BidirectionalHashMap INSTANCE;
    private final HashMap<Node, Graphical> codeGraphical = new HashMap<Node, Graphical>();
    private final HashMap<Graphical, Node> graphicalCode = new HashMap<Graphical, Node>();
    private final HashMap<Port, GConnection> portConnection = new HashMap<Port, GConnection>();

    private static synchronized BidirectionalHashMap tryCreateInstance() {
        if (INSTANCE == null) {
            INSTANCE = new BidirectionalHashMap();
        }
        return INSTANCE;
    }

    public static void setInstance(BidirectionalHashMap bdhm) {
        INSTANCE = bdhm;
    }

    public static BidirectionalHashMap getInstance() {
        BidirectionalHashMap o = INSTANCE;
        if (o == null) {
            o = tryCreateInstance();
        }
        return o;
    }

    public void print() {
        System.out.println("\n");
        for (Node g : codeGraphical.keySet()) {
            System.out.println("\t\t" + g.getClass().getSimpleName() + "(" + g.hashCode() + ") - " + get(g).getClass().getSimpleName() + "(" + get(g).hashCode() + ")");
        }
        System.out.println("\n\n\n");
        for (Graphical g : codeGraphical.values()) {
            System.out.println("\t\t" + g.getClass().getSimpleName() + "(" + g.hashCode() + ") - " + get(g).getClass().getSimpleName() + "(" + get(g).hashCode() + ")");
            for (String s : get(g).getPortMap().keySet()) {
                System.out.println("\t\t\t\t" + s + "-" + get(g).getPort(s).hashCode());
            }
        }
    }

    public void put(Node key, Graphical value) {
        codeGraphical.put(key, value);
        graphicalCode.put(value, key);
    }

    public void put(Graphical key, Node value) {
        graphicalCode.put(key, value);
        codeGraphical.put(value, key);
    }

    public Node get(Graphical key) {
        return graphicalCode.get(key);
    }

    public Graphical get(Node key) {
        return codeGraphical.get(key);
    }

    public GConnection get(Port key) {
        return portConnection.get(key);
    }

    public void remove(Port key) {
        portConnection.remove(key);
    }

    public void put(Port key, GConnection value) {
        portConnection.put(key, value);
    }

    public void remove(Node key) {
        Graphical g = codeGraphical.get(key);
        codeGraphical.remove(key);
        graphicalCode.remove(g);
    }

    public void remove(Graphical key) {
        Node c = graphicalCode.get(key);
        graphicalCode.remove(key);
        codeGraphical.remove(c);
    }

    public void removeAll() {
        graphicalCode.clear();
        codeGraphical.clear();
    }
}
